package simtester.server

class TestSuiteController {

    def list() {
        def catlist = Category.list(sort: 'name').collect { it.name }
        
        def team = User.findByUsername(session.user).team
        def testcases = team.testcases
        testcases = testcases.sort { it.name }
		
        def testsuites = team.testsuites
		def testsuitescount = testsuites.size()
        
        params.sort = params.sort ?: 'name'
		
		if ( testsuitescount != 0 ) {
            // Sorting
            def u = User.findByUsername(session.user)
            testsuites = u.team.testsuites.sort { a,b ->
                if (params.sort == 'owner') {
                    if (params.order == 'desc') {
                        b.owner.name <=> a.owner.name
                    } else {
                        a.owner.name <=> b.owner.name
                    }
                } else {
                    if (params.order == 'desc') {
                        b."${params.sort}" <=> a."${params.sort}"
                    } else {
                        a."${params.sort}" <=> b."${params.sort}"
                    }
                }
            }
            
            // Pagination
			int offset = 0
			int max    = 10

			if (params.offset) {
				offset = params.offset.toInteger()
				max    = params.max.toInteger()
			}
			int end = offset + max - 1
			if (end >= testsuitescount) {
				end = testsuitescount - 1
			}
			testsuites = testsuites[offset..end]
		}
		
        def types = Type.list().collect { it.name }
        
        [testsuites: testsuites, testcases: testcases, catlist: catlist, types: types, activeView: 'testsuites', testsuitescount: testsuitescount]
    }
    
    def create() {
        def user = User.findByUsername(session.user)
        def suite = new TestSuite(name: params.name, owner: user, type: params.type)

        def team = user.team
        team.addToTestsuites(suite)
        team.save(flush:true, failOnError:true)
        
        // Single testcase
        if (params.testcase instanceof java.lang.String) {
            suite.addToTestcases(TestCase.get(params.testcase.toLong()))
        // Multiple testcases
        } else {
            params.testcase.each {
                suite.addToTestcases(TestCase.get(it.toLong()))
            }
        }
        suite.save(flush:true, failOnError:true)

        redirect (action: 'list')
    }
    
    def update() {
        def ts = TestSuite.get(params.tsid.toLong())
        ts.name = params.name
        
        def tmpParams = params.findAll { key, value ->
            key != 'name' &&
            key != 'tsid' &&
            key != 'controller' &&
            key != 'action'
        }

        tmpParams.each { key, value ->
            ts.setCategory(key.toLowerCase(), value)
        }
        
        ts.save(failOnError: true)
        
        redirect(controller: 'testSuite', action: 'list')
    }
    
    def deltc() {
        if (request.method == 'POST') {
            def testcases = []
            if (params['tc[]'] instanceof String) {
                testcases << params['tc[]']
            } else {
                testcases = params['tc[]']
            }
            
            def ts = TestSuite.get(params.id.toLong())
            testcases.each {
                def tc = TestCase.get(it.toLong())
                ts.removeFromTestcases(tc)
            }
            ts.save()

            render (contentType:'text/json') {
                [rc: 0]
            }
        }
    }
    
    def addtc() {
        if (request.method == 'POST') {
            def testcases = []
            if (params['tc[]'] instanceof String) {
                testcases << params['tc[]']
            } else {
                testcases = params['tc[]']
            }
        
            def ts = TestSuite.get(params.id.toLong())
            def currentTests = ts.testcases.collect { it.id.toString() }
            def newTests = testcases - currentTests
            testcases.each {
                def tc = TestCase.get(it.toLong())
                ts.addToTestcases(tc)
            }
            ts.save()
            
            def catlist = Category.list(sort: 'name').collect { it.name }
            def tclist = newTests.collect { tc ->
                def test = TestCase.get(tc.toLong())
                def cats = []
                catlist.each {
                    def val = test.findCatValue(it)
                    if (val) {
                        cats << val
                    } else {
                        cats << ' '
                    }
                }
                [id: test.id.toString(), name: test.name, cats: cats, owner: test.owner.username, date: test.dateCreated.time, type: test.type]
            }
            
            tclist = tclist.sort { it.name }
            
            render (contentType:'text/json') {
                [tclist: tclist]
            }
        }
    }
	
	def delete() {
		def ts = TestSuite.get(params.tsid.toLong())
		ts.delete()
		redirect(action: 'list')
	}
}
